#pragma once

#include<iostream>
#include<string>
#include<cstring>
#include"LockGuard.hpp"

using namespace std;

typedef void*(*func_t)(void*);

typedef enum
{
  RUNNING = 1,
  NEW,
  EXIT
}ThreadStatus;

class Thread
{
public:
  Thread(int num,func_t func,void* args)
    :_func(func)
    ,_args(args)
    ,_thread(0)
    ,_status(NEW)
  {
    char buffer[24];
    snprintf(buffer,sizeof(buffer),"thread->%d",num);
    _name = buffer;
  }

  void RunHelper()
  {
    
  }

  void join()
  {
    int n = pthread_join(_thread,nullptr);
    if(n != 0)
    {
      cout << "Thread join fail" << endl;
      exit(1);
    }
    _status = EXIT;
  }

  void run()
  {
    int n = pthread_create(&_thread,nullptr,_func,_args);//也可以不使用RunHelper，Thread里面有使用  
    if(n != 0) exit(2);
    _status = RUNNING;
  }

  int status() {return _status;}

  string& name() {return _name;}

  pthread_t thread()
  {
    if(_status == RUNNING)
    {
      return _thread;
    }
    else
    {
      cout << "This thread is not running" << endl;
      return 0;
    }
  }

  ~Thread()
  {}
private:
  pthread_t _thread;
  string _name;
  func_t _func;
  void* _args;
  ThreadStatus _status;
};
